/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import DTO.GaTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class GaTau_DAO {
    
     public static String Get_MaGT() throws SQLException{
        String MaGT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "GT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaGT = Rs.getString(1);
        }
        return MaGT;
    }
    
    public static ArrayList<GaTau_DTO> GetALL()
    {
        ArrayList<GaTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from GaTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                GaTau_DTO GaTau = new GaTau_DTO();
                GaTau.setMaGa(rs.getString("MaGa"));
                GaTau.setTenGa(rs.getString("TenGa"));
                ds.add(GaTau);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(GaTau_DTO GaTau)
    {
        boolean flag = false;
        try {
            String sql = "Insert GaTau Values('"+GaTau.getMaGa()+"','"+GaTau.getTenGa()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(GaTau_DTO GaTau)
    {
        boolean flag = false;
        try {
            // Chu y Cap Nhat.
            String sql = "Update GaTau Set TenGa = '"+ GaTau.getTenGa() +"' where MaGa = '"+ GaTau.getMaGa()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maGa)
    {
        boolean flag = false;
        try {
            String sql = "Delete from GaTau where MaGa = '"+maGa+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
